﻿using AutoMapper;
using HelenServer.FileStorage.Contracts;
using HelenServer.Grpc.Contracts;

namespace HelenServer.FileStorage.Services;

[Injection(typeof(IFSUploadFile))]
internal class FSUploadFile : GrpcMethod<UploadGrpcModel, GrpcResult<int>>, IFSUploadFile
{
    private readonly IFileStorageService _service;
    private readonly IMapper _mapper;

    public FSUploadFile(IFileStorageService fileStorage, IMapper mapper)
    {
        _service = fileStorage;
        _mapper = mapper;
    }

    public override async Task<GrpcResult<int>> InvokeAsync(UploadGrpcModel request, ServerCallContext context)
    {
        var model = _mapper.Map<UploadGrpcModel, UploadModel>(request);

        var operation = context.GetOperation(model);

        var result = await _service.UploadFileAsync(operation, context.CancellationToken);

        return result.ToGrpcResult(r => r);
    }
}